﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.User
{
    public class GroupBL
    {
        protected void clsGroupInsert(tbl_Group groups)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_Group group = new tbl_Group();
                    group.GroupName = groups.GroupName;
                    group.GroupDescription = groups.GroupDescription;
                    group.CreatedDate = DateTime.Now;
                    group.CreatedUser = groups.CreatedUser;
                    group.Status = groups.Status;

                    adlerContextDb.tbl_Groups.InsertOnSubmit(group);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsgroupModify(tbl_Group groups)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var group = adlerContextDb.tbl_Groups.Single(d => d.GroupNo == groups.GroupNo);
                    group.GroupName = groups.GroupName;
                    group.GroupDescription = groups.GroupDescription;
                    group.Status = groups.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsgroupDelete(tbl_Group groups)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var group = adlerContextDb.tbl_Groups.Single(d => d.GroupNo == groups.GroupNo);
                    group.Status = groups.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        public List<tbl_Group> GetGroup()
        {
            AdlerContextDataContext adlerContextDb = new AdlerContextDataContext();
            return adlerContextDb.tbl_Groups.ToList();
        }

        // Show Group.
        //public List<MODEL_SP_ADMIN_SEARCH_GROUP> Get_Admin_Group(string groupname, string status)
        //{
        //    using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
        //    {
        //        int index = 1;
        //        var query = from n in contextdb.SP_ADMIN_SEARCH_GROUP(groupname, status)
        //                    select new MODEL_SP_ADMIN_SEARCH_GROUP
        //                    {
        //                        Index = index++,
        //                        GroupNo = n.GroupNo,
        //                        GroupName = n.GroupName,
        //                        GroupDescription = n.GroupDescription,
        //                        CreatedDate = n.CreatedDate,
        //                        CreatedUser = n.CreatedUser,
        //                        Status = n.Status
        //                    };
        //        return query.ToList();
        //    }
        //}

        public tbl_Group GetGroupsByGroupno(int groupno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_Groups.Single(d => d.GroupNo == groupno);
            }
        }
    }
}
